import datetime

from lib.response import response
# from .user import host
from . import db

class cpu_t(db.Model):
    __tablename__ = "cpu"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    hostname = db.Column(db.String(100), db.ForeignKey('host.hostname'), nullable=False)
    date = db.Column(db.DateTime, nullable=True)
    cpu_core = db.Column(db.String(15), nullable=True)
    cpu_used = db.Column(db.String(8), nullable=True)
    cpu_free = db.Column(db.String(8), nullable=True)

    def keys(self):
        return ("id","hostname","date","cpu_core","cpu_used","cpu_free")

    def __getitem__(self, key):
        # print(getattr(self,key),type(getattr(self,key)))
        if key == "date":
            return str(getattr(self,key))
        return getattr(self,key)

    @classmethod
    def create(cls, hostname, date, cpu_core, cpu_used, cpu_free):
        record = cls(
            hostname=hostname,
            date=date,
            cpu_core=cpu_core,
            cpu_used=cpu_used,
            cpu_free=cpu_free
        )
        db.session.add(record)
        db.session.commit()
        return record

    @classmethod
    def get(cls):
        records = cls.query.all()
        recod_lst = [dict(record) for record in records]
        return recod_lst

    @classmethod
    def get_by_id(cls, id):
        record = cls.query.get(id)
        tmp_dict = dict(record)
        return tmp_dict

    @classmethod
    def get_by_hostname(cls, hostname):
        return cls.query.filter_by(hostname=hostname).all()

    @classmethod
    def update(cls, id, hostname, date, cpu_core, cpu_used, cpu_free):
        record = cls.query.get(id)
        if not record:
            return None

        record.hostname = hostname
        record.date = date
        record.cpu_core =cpu_core
        record.cpu_used = cpu_used
        record.cpu_free = cpu_free

        db.session.commit()
        return record

    @classmethod
    def delete(cls, id):
        record = cls.query.get(id)
        if record:
            db.session.delete(record)
            db.session.commit()
            return True
        return False

class mem_t(db.Model):
    __tablename__ = "mem"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    hostname = db.Column(db.String(100),db.ForeignKey('host.hostname') ,nullable=False)
    date = db.Column(db.DateTime, nullable=True)
    used = db.Column(db.Integer, nullable=True)
    free = db.Column(db.Integer, nullable=True)
    total = db.Column(db.Integer, nullable=True)
    used_percent = db.Column(db.String(8), nullable=True)

    def keys(self):
        return ("id","hostname","date","used","free","total","used_percent")

    def __getitem__(self, key):
        if key == "date":
            return str(getattr(self,key))
        return getattr(self,key)

    @classmethod
    def create(cls, hostname, date, used, free, total, used_percent):
        record = cls(
            hostname=hostname,
            date=date,
            used=used,
            free=free,
            total=total,
            used_percent=used_percent
        )
        db.session.add(record)
        db.session.commit()
        return record

    @classmethod
    def get(cls):
        records = cls.query.all()
        recod_lst = [dict(record) for record in records]
        return recod_lst

    @classmethod
    def get_by_id(cls, id):
        record = cls.query.get(id)
        tmp_dict = dict(record)
        return tmp_dict

    @classmethod
    def get_by_hostname(cls, hostname):
        return cls.query.filter_by(hostname=hostname).all()

    @classmethod
    def update(cls, id, hostname, date, used, free, total, used_percent):
        record = cls.query.get(id)
        if not record:
            return None

        record.hostname = hostname
        record.date = date
        record.used = used
        record.free = free
        record.total = total
        record.used_percent = used_percent

        db.session.commit()
        return record

    @classmethod
    def delete(cls, id):
        record = cls.query.get(id)
        if record:
            db.session.delete(record)
            db.session.commit()
            return True
        return False

class disk_t(db.Model):
    __tablename__ = "disk"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    hostname = db.Column(db.String(100), db.ForeignKey('host.hostname'), nullable=False)
    date = db.Column(db.DateTime, nullable=True)
    f_type = db.Column(db.String(20), nullable=True)
    used = db.Column(db.Integer, nullable=True)
    free = db.Column(db.Integer, nullable=True)
    total = db.Column(db.Integer, nullable=True)
    used_percent = db.Column(db.String(8), nullable=True)
    mountpoint = db.Column(db.String(40), nullable=True)

    def keys(self):
        return ("id","hostname","date","f_type","used","free","total","used_percent","mountpoint")

    def __getitem__(self, key):
        if key == "date":
            return str(getattr(self,key))
        return getattr(self,key)

    @classmethod
    def create(cls, hostname, date, f_type, used, free, total, used_percent, mountpoint):
        record = cls(
            hostname=hostname,
            date=date,
            f_type=f_type,
            used=used,
            free=free,
            total=total,
            used_percent=used_percent,
            mountpoint=mountpoint
        )
        db.session.add(record)
        db.session.commit()
        return record

    @classmethod
    def get(cls):
        records = cls.query.all()
        recod_lst = [dict(record) for record in records]
        return recod_lst

    @classmethod
    def get_by_id(cls, id):
        record = cls.query.get(id)
        tmp_dict = dict(record)
        return tmp_dict

    @classmethod
    def get_by_hostname(cls, hostname):
        return cls.query.filter_by(hostname=hostname).all()

    @classmethod
    def get_by_mountpoint(cls, hostname, mountpoint):
        return cls.query.filter_by(hostname=hostname, mountpoint=mountpoint).all()

    @classmethod
    def update(cls, id, hostname, date, f_type, used, free, total, used_percent, mountpoint):
        record = cls.query.get(id)
        if not record:
            return None

        record.hostname = hostname
        record.date = date
        record.f_type = f_type
        record.used = used
        record.free = free
        record.total = total
        record.used_percent = used_percent
        record.mountpoint = mountpoint

        db.session.commit()
        return record

    @classmethod
    def delete(cls, id):
        record = cls.query.get(id)
        if record:
            db.session.delete(record)
            db.session.commit()
            return True
        return False

